﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace wpf.Triage.Behavior
{
    public class CanvasDrawBehavio : Behavior<Canvas>
    {
        #region 绘制集合

        List<UIElement> uIElements = new List<UIElement>();
        /// <summary>
        /// 可拖动行为
        /// </summary>
        DragInCanvasBehavior drag = new DragInCanvasBehavior();
        #endregion

        #region 绘制相关属性

        /// <summary>
        /// 是否绘制过程中
        /// </summary>
        private bool isDrawing;
        /// <summary>
        ///   //绘制起始点
        /// </summary>
        private Point startPoint = new Point();
        /// <summary>
        /// //基于原点的坐标
        /// </summary>
        private Point leftPoint = new Point();
        #endregion



        #region 依赖属性
        /// <summary>
        /// 编辑触发事件
        /// </summary>
        public Action<dynamic> EditCommand
        {
            get { return (Action<dynamic>)GetValue(EditCommandProperty); }
            set { SetValue(EditCommandProperty, value); }
        }

        public static readonly DependencyProperty EditCommandProperty =
            DependencyProperty.Register("EditCommand", typeof(Action<dynamic>), typeof(CanvasDrawBehavio), new PropertyMetadata(null));

        #endregion

        /// <summary>
        ///附加事件
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.MouseLeftButtonDown += canvas_MouseDown;
            AssociatedObject.MouseUp += canvas_MouseUp;
            AssociatedObject.MouseMove += canvas_MouseMove;
            //添加控件

        }
        /// <summary>
        /// 移除事件
        /// </summary>
        protected override void OnDetaching()
        {
            AssociatedObject.MouseLeftButtonDown -= canvas_MouseDown;
            AssociatedObject.MouseUp -= canvas_MouseUp;
            AssociatedObject.MouseMove -= canvas_MouseMove;
        }

        /// <summary>
        /// 鼠标按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                startPoint = e.GetPosition(e.Source as FrameworkElement);//WPF方法
                SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 140, 0));
                isDrawing = true;
            }
        }
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrawing)
            {
                Point currentPoint = e.GetPosition(e.Source as FrameworkElement);//WPF方法
                double width = currentPoint.X - startPoint.X;
                double height = currentPoint.Y - startPoint.Y;

                DrawRect drawRect = new DrawRect();
                drawRect.Width = Math.Abs(width);
                drawRect.Height = Math.Abs(height);
                if (width < 0)
                {
                    drawRect.X = currentPoint.X;
                }
                else
                {
                    drawRect.X = startPoint.X;
                }
                if (height < 0)
                {
                    drawRect.Y = currentPoint.Y;
                }
                else
                {
                    drawRect.Y = startPoint.Y;
                }
                if (EditCommand!=null)
                {
                    EditCommand(drawRect);
                }
            }
        }
        /// <summary>
        /// 鼠标弹起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void canvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (isDrawing)
            {
                isDrawing = false;
                //removeEvent();
            }
        }
    }

    public class DrawRect
    {

        public double Width { get; set; }
        public double Height { get; set; }
        public double X { get; set; }
        public double Y { get; set; }
    }
}
